﻿using Group3.MediaTool.Data.Generator;
using Group3.MediaTool.Data.IO;
using Group3.MediaTool.Generator;
using Group3.MediaTool.Interface;
using Group3.MediaTool.Provider;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Group3.MediaTool.ConsoleRunner
{
    class StartApplication
    {
        DataDefinitionIO ddIO = new DataDefinitionIO();

        public StartApplication()
        {
            loadConfig();
            if(StaticConfig.Database == e_DataStoreProvider.MSSQLServer)
            {
                checkSQLDatabase();
            }
            createObjects();
        }

        private void loadConfig()
        {
            List<KeyValuePair<String,String>> config = ddIO.readConfig();

            var database = from c in config
                           where c.Key.ToUpper() == "DATABASE"
                           select c.Value;
            StaticConfig.Database = (e_DataStoreProvider)(Convert.ToInt32(database.First()));

            var dataDefinitionFile = from c in config
                                     where c.Key.ToUpper() == "DATADEFINITIONFILE"
                           select c.Value;

            StaticConfig.pathDataDefinitionFile = dataDefinitionFile.First();


            var dataDefinitionFolder = from c in config
                                       where c.Key.ToUpper() == "DATADEFINITIONFOLDER"
                                       select c.Value;

            StaticConfig.pathDataDefinition = dataDefinitionFolder.First();

            var databaseCon = from c in config
                                       where c.Key.ToUpper() == "DATABASECONNECTION"
                                       select c.Value;
            StaticConfig.databaseConnection = databaseCon.First();


        }

        private void createObjects()
        {
            DataDefinitionClass dd = new DataDefinitionClass();
            ClassBuilder cb = new ClassBuilder();
            dd.createObjects();

            foreach (IObjectDefinition itm in dd.Objects)
            {
                cb.BuildClass(itm);
            }
            ObjectProvider.Instance = new ObjectProvider();
            ObjectProvider.Instance.DoImport();
        }

        private void checkSQLDatabase()
        {
            //
            SQLDatabaseProvider sqlConn = new SQLDatabaseProvider();
            sqlConn.Connect = StaticConfig.databaseConnection;
            sqlConn.createDatabase();
            sqlConn.dropAllTabels();

            return; 
        }

    }
}
